$(function () {

    initArtCate()

    function initArtCate() {
        $.ajax({
            method: 'GET',
            url: '/my/article/cates',
            success: res => {
                let htmlStr = template('tpl-table', res)
                $('tbody').html(htmlStr)
            }
        })
    }
    // 添加类别 
    let indexAdd = null
    $('#btnAddCate').on('click', function () {
        indexAdd = layui.layer.open({
            title: '添加文章类别',
            area: ['500px', '250px'],
            type: 1,
            content: $('#dialog-add').html()
        })
    })

    // 使用事件委托
    $('body').on('submit', '#form-add', function (e) {
        e.preventDefault()
        $.ajax({
            method: 'POST',
            url: '/my/article/addcates',
            data: $(this).serialize(),
            success: res => {
                const {
                    status,
                } = res
                if (status !== 0) {
                    return layui.layer.msg('添加类别失败！')
                }
                initArtCate()
                layui.layer.msg('添加类别成功！')
                layui.layer.close(indexAdd)
            }
        })
    })

    $('tbody').on('click', '.btn-delete', function () {
        var id = $(this).attr('data-id')
        layui.layer.confirm('确认删除?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            $.ajax({
                method: 'GET',
                url: '/my/article/deletecate/' + id,
                success: res => {
                    const {
                        status
                    } = res
                    if (res.status !== 0) {
                        return layui.layer.msg('删除类别失败！')
                    }
                    layui.layer.msg('删除类别成功！')
                    layui.layer.close(index)
                    initArtCate()
                }
            })
        })
    })

    // 修改文章类别
    let indexEdit = null
    $('tbody').on('click', '.btn-edit', function () {
        indexEdit = layui.layer.open({
            title: '修改文章类别',
            area: ['500px', '250px'],
            type: 1,
            content: $('#dialog-edit').html()
        })

        let id = $(this).arrt('data-id')
        if (id < 2) return layui.layer.msg('不能修改')
        $.ajax({
            method: 'GET',
            url: '/my/article/cates/:id' + id,
            success: res => {
                layui.form.val() = ('form-edit', res.data)
            }
        })
    })

    $('body').on('submit', '#form-edit', function (e) {
        e.preventDefault()
        $.ajax({
            method: 'POST',
            url: "/my/article/updatecate",
            data: $(this).serialize(),
            success: res => {
                const {
                    status
                } = res
                if (status !== 0) {
                    return layui.layer.msg('更新分类数据失败！')
                }
                layui.layer.msg('更新分类数据成功！')
                layui.layer.close(indexEdit)
                initArtCate()
            }
        })
    })
})